package com.school.controller;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.util.WebUtils;

import com.alibaba.fastjson.JSONObject;
import com.school.entity.web.HljtAuthorityManagement;
import com.school.service.HljtAuthorityManagementService;
import com.school.util.JsonUtil;
import com.school.util.PictureVideoUpload;
import com.school.util.RepUtil;
import com.school.util.WhetherNotToUpdate;

@Controller
public class HljtAuthorityManagementController {
	@Autowired
	private HljtAuthorityManagementService hljtAuthorityManagementService;
	
	
	/**
	 * 添加教师 并分配权限  参数data
	 * @param hljtSchoolimage
	 * @param jsonUtil
	 * @return
	 */
	@RequestMapping("/teacherInformation")
	@ResponseBody
	public JSONObject teacherInformation(HttpServletResponse rep , HttpServletRequest request, HljtAuthorityManagement hljtAuthorityManagement,JsonUtil jsonUtil) {
		JSONObject ret=new JSONObject();
		try {
			RepUtil.repPake(rep);
			if(null !=hljtAuthorityManagement.getTeacherId()) {
				//根据老师id查询相关信息
				HljtAuthorityManagement temp = hljtAuthorityManagementService.authorityManagementByTeacherId(hljtAuthorityManagement);
				//jsonUtil.setSucceed(temp , "老师的信息"  );
				temp.setTeacherId(temp.getId());
				temp.setImagePath(WhetherNotToUpdate.getIp()+temp.getImagePath());
				ret.put("teacherInfo", temp);
			
			}else {
				ret.put("teacherInfo", "teacherId为空");
			}
		} catch (Exception e) {
			e.printStackTrace();
			//jsonUtil.setFailMsg("服务器异常");
			ret.put("serverException", "服务器异常");
		}
		return ret;
	}
	
	/**
	 * 添加教师 并分配权限  参数data
	 * @param hljtSchoolimage
	 * @param jsonUtil
	 * @return
	 */
	@RequestMapping("/addOrUpdateTeacher")
	@ResponseBody
	public JSONObject addOrUpdateTeacher(HttpServletResponse rep , HttpServletRequest request, HljtAuthorityManagement hljtAuthorityManagement,JsonUtil jsonUtil) {
		JSONObject ret=new JSONObject();
		try {
			RepUtil.repPake(rep);
			boolean isMultipart = ServletFileUpload.isMultipartContent(request);
			 MultipartFile file=null;
			if (isMultipart){ 
			    MultipartHttpServletRequest multipartRequest = WebUtils.getNativeRequest(request, MultipartHttpServletRequest.class);
			     file = multipartRequest.getFile("imgFile");
			     String imagePath = PictureVideoUpload.savePictureVideo(file, WhetherNotToUpdate.getSaveDisk());
					hljtAuthorityManagement.setImagePath(imagePath);
			}
			if(null ==hljtAuthorityManagement.getTeacherId()) {
			//执行添加返回老师自增Id
			Integer temp =	hljtAuthorityManagementService.addTeacherAuthority(hljtAuthorityManagement);
			if(null !=temp) {
				ret.put("teacherId", temp);
				ret.put("success", true);
			}else {
				ret.put("success", false);
			}
		}else {
			Integer temp =	hljtAuthorityManagementService.updateTeacherByTeacherId(hljtAuthorityManagement);
			if(null !=temp) {
				ret.put("success", true);
				ret.put("teacherId", hljtAuthorityManagement.getTeacherId());
			}else {
				ret.put("success", false);
				ret.put("teacherId", hljtAuthorityManagement.getTeacherId());
			}
		}
		} catch (Exception e) {
			e.printStackTrace();
			ret.put("success", false);
			ret.put("serverException", "服务器异常");
		}
		return ret;
	}
	

	/**
	 * 修改教师 配权限  参数data
	 * @param hljtSchoolimage
	 * @param jsonUtil
	 * @return
	 */
	@RequestMapping(value ="/teacherList",method = RequestMethod.GET)
	@ResponseBody
	public JSONObject teacherList (HttpServletResponse rep ,String data , HljtAuthorityManagement hljtAuthorityManagement, JsonUtil jsonUtil) {
		JSONObject ret=new JSONObject();
		try {
			RepUtil.repPake(rep);
			hljtAuthorityManagement.setPageNum((hljtAuthorityManagement.getPageIndex()-1)*hljtAuthorityManagement.getPageSize());
			ret.put("data", hljtAuthorityManagementService.allAuthorityManagement(hljtAuthorityManagement) );
		} catch (Exception e) {
			e.printStackTrace();
			ret.put("serverException", "服务器异常");
		}
		return ret;
	}
	
	
	
	
	
	/**
	 * 添加教师 并分配权限  参数data
	 * @param hljtSchoolimage
	 * @param jsonUtil
	 * @return
	 */
	@RequestMapping("/addOrUpdateTeacherPer")
	@ResponseBody
	public JSONObject addOrUpdateTeacherPer(HttpServletResponse rep , HttpServletRequest request, HljtAuthorityManagement hljtAuthorityManagement,JsonUtil jsonUtil) {
		JSONObject ret=new JSONObject();
		try {
			RepUtil.repPake(rep);
			boolean isMultipart = ServletFileUpload.isMultipartContent(request);
			 MultipartFile file=null;
			if (isMultipart){ 
			    MultipartHttpServletRequest multipartRequest = WebUtils.getNativeRequest(request, MultipartHttpServletRequest.class);
			     file = multipartRequest.getFile("imgFile");
			     String imagePath = PictureVideoUpload.savePictureVideo(file, WhetherNotToUpdate.getSaveDisk());
					hljtAuthorityManagement.setImagePath(imagePath);
			}
			if(null ==hljtAuthorityManagement.getTeacherId()) {
				ret.put("teacherId", "teacherId为空");
			}else {
			Integer temp =	hljtAuthorityManagementService.updateTeacherByTeacherIdPer(hljtAuthorityManagement);
			if(null !=temp) {
				ret.put("success", true);
				ret.put("teacherId", hljtAuthorityManagement.getTeacherId());
			}else {
				ret.put("success", false);
				ret.put("teacherId", hljtAuthorityManagement.getTeacherId());
			}
		}
		} catch (Exception e) {
			e.printStackTrace();
			ret.put("serverException", "服务器异常");
		}
		return ret;
	}
	
	
	
	
	/**
	 * 添加教师 并分配权限  参数data
	 * @param hljtSchoolimage
	 * @param jsonUtil
	 * @return
	 */
	@RequestMapping("/deletetTeacherAuth")
	@ResponseBody
	public JSONObject deletetTeacherAuth(HttpServletResponse rep , HttpServletRequest request, HljtAuthorityManagement hljtAuthorityManagement,JsonUtil jsonUtil) {
		JSONObject ret=new JSONObject();
		try {
			RepUtil.repPake(rep);
			if(null ==hljtAuthorityManagement.getTeacherId()&&null==hljtAuthorityManagement.getRoleDelId()) {
				ret.put("parameter", "参数不对");
			}else {
			if(hljtAuthorityManagementService.deletetTeacherAuth(hljtAuthorityManagement)) {
				ret.put("success", true);
			}else {
				ret.put("success", false);
			}
		}
		} catch (Exception e) {
			e.printStackTrace();
			ret.put("serverException", "服务器异常");
		}
		return ret;
	}
	
	
	
}
